import { TAB_VIEWS } from '@/store/mutationTypes'
import { createStorage } from '@/utils/storage'

const Storage = createStorage({ storage: sessionStorage })

// 将tabview存储到缓存内，保存当前tabview
// 展开vue-router实例，只提取 name | fullPath | path | meta | query | title
const setLocatTabViews = (routes = []) => {
  const locatRoutes = routes.map((routeItem) => {
    const { name, fullPath, path, meta, query, title } = routeItem
    return { name, fullPath, path, meta, query, title }
  })
  Storage.set(TAB_VIEWS, locatRoutes, null)
}

const StoreModel = {
  namespaced: true,
  state: {
    visitedRoutes: Storage.get(TAB_VIEWS, [])
  },
  getters: {
    visitedRoutes: (state) => state.visitedRoutes
  },
  mutations: {
    addVisitedRoute(state, route) {
      const target = state.visitedRoutes.find((item) => item.path === route.path)
      if (target) {
        if (route.fullPath !== target.fullPath) Object.assign(target, route)
        return
      }
      state.visitedRoutes.push(Object.assign({}, route))
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    delVisitedRoute(state, route) {
      state.visitedRoutes.forEach((item, index) => {
        if (item.path === route.path) state.visitedRoutes.splice(index, 1)
      })
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    delOthersVisitedRoute(state, route) {
      state.visitedRoutes = state.visitedRoutes.filter((item) => item.meta.affix || item.path === route.path)
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    delLeftVisitedRoute(state, route) {
      let index = state.visitedRoutes.length
      state.visitedRoutes = state.visitedRoutes.filter((item) => {
        if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
        return item.meta.affix || index <= state.visitedRoutes.indexOf(item)
      })
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    delRightVisitedRoute(state, route) {
      let index = state.visitedRoutes.length
      state.visitedRoutes = state.visitedRoutes.filter((item) => {
        if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
        return item.meta.affix || index >= state.visitedRoutes.indexOf(item)
      })
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    delAllVisitedRoutes(state) {
      state.visitedRoutes = state.visitedRoutes.filter((item) => item.meta.affix)
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    },
    updateVisitedRoute(state, route) {
      state.visitedRoutes.forEach((item) => {
        if (item.path === route.path) item = Object.assign(item, route)
      })
      // 保存tabview缓存
      setLocatTabViews(state.visitedRoutes)
    }
  },
  actions: {
    /**
     * 添加tab路由
     */
    addVisitedRoute({ commit }, route) {
      commit('addVisitedRoute', route)
    },

    /**
     * 关闭tab路由
     */
    async delRoute({ dispatch, state }, route) {
      await dispatch('delVisitedRoute', route)
      return {
        visitedRoutes: [...state.visitedRoutes]
      }
    },
    delVisitedRoute({ commit, state }, route) {
      commit('delVisitedRoute', route)
      return [...state.visitedRoutes]
    },

    /**
     * 关闭其他tab路由
     */
    async delOthersRoutes({ dispatch, state }, route) {
      await dispatch('delOthersVisitedRoute', route)
      return {
        visitedRoutes: [...state.visitedRoutes]
      }
    },
    delOthersVisitedRoute({ commit, state }, route) {
      commit('delOthersVisitedRoute', route)
      return [...state.visitedRoutes]
    },

    /**
     * 关闭左侧tab路由
     */
    async delLeftRoutes({ dispatch, state }, route) {
      await dispatch('delLeftVisitedRoute', route)
      return {
        visitedRoutes: [...state.visitedRoutes]
      }
    },
    delLeftVisitedRoute({ commit, state }, route) {
      commit('delLeftVisitedRoute', route)
      return [...state.visitedRoutes]
    },

    /**
     * 关闭右侧tab路由
     */
    async delRightRoutes({ dispatch, state }, route) {
      await dispatch('delRightVisitedRoute', route)
      return {
        visitedRoutes: [...state.visitedRoutes]
      }
    },
    delRightVisitedRoute({ commit, state }, route) {
      commit('delRightVisitedRoute', route)
      return [...state.visitedRoutes]
    },

    /**
     * 关闭全部tab路由
     */
    async delAllRoutes({ dispatch, state }, route) {
      await dispatch('delAllVisitedRoutes', route)
      return {
        visitedRoutes: [...state.visitedRoutes]
      }
    },
    delAllVisitedRoutes({ commit, state }) {
      commit('delAllVisitedRoutes')
      return [...state.visitedRoutes]
    },

    /**
     * 更新tab
     */
    updateVisitedRoute({ commit }, route) {
      commit('updateVisitedRoute', route)
    }
  }
}

export default StoreModel
